package com.wzxy.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wzxy.model.Department;
import com.wzxy.service.IDepartmentService;
import com.wzxy.utils.Page;
import com.wzxy.views.ViewExcel;

@Controller
@RequestMapping("/department")
public class DepartmentController {

	@Resource
	private IDepartmentService departmentService;
		
	
	/**
	 * 显示部门列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list(Model model,HttpServletRequest request) {	
		String pageNo = request.getParameter("pageNo");
		if (pageNo == null) {
            pageNo = "1";
        }
		@SuppressWarnings("rawtypes")
		Page page = departmentService.queryForPage(Integer.valueOf(pageNo),5, "from Department");
		request.setAttribute("page", page);
		@SuppressWarnings("unchecked")
		List<Department>list = page.getList();		
		model.addAttribute("list", list);
		return "department_list";
	}

	/**
	 * 新建部门
	 * 
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Department department) {
		departmentService.add(department);
		return "redirect:/department/list";
	}
	
	/**
	 * 跳转到更新页面，并把需要更新的数据在更新页面显示
	 * @param did
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate",method = RequestMethod.GET)
	public String toUpdate(String did,Model model){
		Department department = departmentService.load(Integer.valueOf(did));
		model.addAttribute("did", department.getDid());
		model.addAttribute("dname", department.getDname());
		model.addAttribute("ddesc", department.getDdesc());
		return "update_department";
	}
	
	/**
	 * 更新一条记录
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public String update(Department department){
		System.out.println("更新的方法---->"+department.getDname()+"----->"+department.getDdesc());
		departmentService.update(department);
		return "redirect:/department/list";
	}
	
	/**
	 * 将所有的数据导出到excel
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public ModelAndView export(ModelMap model, HttpServletRequest request) {
		List<Department> list = departmentService.list("from Department", null);
		ViewExcel viewExcel = new ViewExcel();
		// 将查询出的list集合存入ModelMap 对象中，此时的key就是ViewExcel类中Map所对应的key
		model.put("list", list);
		return new ModelAndView(viewExcel, model);
	}
	
	/**
	 * 删除一条记录
	 * @param department
	 * @return
	 */
	@RequestMapping(value = "/delete",method = RequestMethod.GET)
	public String delete(Department department){
		System.out.println("----->需要删除部门的部门编号为："+department.getDid());
		departmentService.delete(department.getDid());
		return "redirect:/department/list";
	}
	
	/**
	 * 根据部门名称查询部门信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/load",method = RequestMethod.POST)
	public String load(String dname,Model model){
		String hql = "from Department where dname = '"+dname+"'";
		List<Department>list = departmentService.list(hql, null);
		model.addAttribute("list", list);
		return "department_list";
	}

}
